package com.wxy.controller;

import cn.hutool.core.bean.BeanUtil;
import com.wxy.domain.*;
import com.wxy.service.ManagerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 47868
 */
@Controller
@RequestMapping("manager")
public class ManagerController {
    private final ManagerService managerService;
    @Autowired
    public ManagerController(ManagerService managerService) {
        this.managerService = managerService;
    }

    @Operation(summary = "借阅记录查询",description = "管理员登录的借阅记录查询")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @GetMapping("/borrowrecord/{searchTerm}")
    @ResponseBody
    public R borrowrecord(@PathVariable("searchTerm") String searchTerm,
                                                 HttpServletRequest request) {
        Map<String,Object> usermap=(Map<String, Object>) request.getAttribute("body");
        int id=(int)usermap.get("userId");
        if(id!=0){
            throw new MyException("系统异常，请重新登录或联系管理员解决");
        }
        return managerService.getAllBorrowInfo(searchTerm);
    }

    @Operation(summary = "用户展示",description = "所有用户展示")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @GetMapping("/alluser/{searchTerm}")
    @ResponseBody
    public R getAlluser(@PathVariable("searchTerm") String searchTerm) {
        return managerService.getAllUser(searchTerm);
    }

    @Operation(summary = "添加图书",description = "添加图书")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @PostMapping("/addBook/{bookName}/{author}/{nums}")
    @ResponseBody
    public R addBook(HttpServletRequest request,@PathVariable("bookName") String bookName,
                     @PathVariable("author") String author,@PathVariable("nums") int nums) {
        Map<String,Object> usermap=(Map<String, Object>) request.getAttribute("body");
        int id=(int)usermap.get("userId");
        if(id!=0){
            throw new MyException("系统异常，请重新登录或联系管理员解决");
        }
        return managerService.addBook(bookName,author,nums);
    }

    @Operation(summary = "删除书籍信息",description = "删除书籍信息成功")
    @ApiResponse(responseCode = "200",description = "响应成功",content = @Content)
    @RequestMapping("/deleteBook/{bid}")
    @ResponseBody
    public R deleteBook(HttpServletRequest request,@PathVariable("bid") int bid) {
        Map<String,Object> usermap=(Map<String, Object>) request.getAttribute("body");
        int id=(int)usermap.get("userId");
        if(id!=0){
            throw new MyException("系统异常，请重新登录或联系管理员解决");
        }
        return managerService.deleteBook(bid);
    }
}
